<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>地址簿 Address Book</title>
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <!-- <script src="js/bootstrap.min.js"></script>-->
    </head>
    <body>
        <div class="container">
            <div class="row">
                <h3><a href="index.php">地址簿 Address Book</a></h3>
            </div>
            <div class="row">
                <p>
                    <a href="create.php" class="btn btn-success">创造 Create</a>
                </p>

                <?php
                    /**
                     * Copyright (c) 2018,2345
                     * 摘    要：首页程序
                     * 作    者：林幸亿
                     * 修改日期：2018.04.20
                     */
                    require_once 'libraries/MysqlDbClass.php';
                    require_once 'libraries/RedisDbClass.php';

                    require_once 'libraries/PaginatorClass.php';

                    /*
                     * Retrieve all the user details from DB and display it all in the table
                     */
                    $db = Mysqldb::getInstance();
                    //$conn = $db->getConnection();
                    //$db->disconnect();
                    //$redis = RedisDb::getInstance();
                    //$data = $redis->hgetall('user:80');
                    //print_r($data); // returns all key-value that belongs to the hash

                    $sql = 'SELECT id, name, email, mobile FROM users'; //ORDER BY id DESC

                    $Paginator = new Paginator($db, $sql);

                    $limit = ( isset($_GET['limit']) ) ? $_GET['limit'] : 10;
                    $page = ( isset($_GET['page']) ) ? $_GET['page'] : 1;
                    $links = ( isset($_GET['links']) ) ? $_GET['links'] : 15;

                    $results = $Paginator->getData($page, $limit);

                    echo $Paginator->createLinks($links, 'pagination pagination-sm');
                ?>    

                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>姓名 <br>Name</th>
                            <th>电子邮件 <br>Email Address</th>
                            <th>手机号码 <br>Mobile Number</th>
                            <th>行动 <br>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ($results->data as $row) {
                            //foreach ($db->finds($sql) as $row)
                            echo '<tr>';
                            echo '<td>' . $row['id'] . '</td>';
                            echo '<td>' . $row['name'] . '</td>';
                            echo '<td>' . $row['email'] . '</td>';
                            echo '<td>' . $row['mobile'] . '</td>';
                            echo '<td><a class="btn" href="read.php?id=' . $row['id'] . '">选择 Read</a>';
                            echo ' ';
                            echo '<a class="btn btn-success" href="update.php?id=' . $row['id'] . '">修改 Update</a>';
                            echo ' ';
                            echo '<a class="btn btn-danger" href="delete.php?id=' . $row['id'] . '">删除 Delete</a>';
                            echo '</td>';
                            echo '</tr>';
                        }

                        //Database::disconnect();
                        ?>
                    </tbody>
                </table>
                <?php echo $Paginator->createLinks($links, 'pagination pagination-sm'); ?> 


<?php
/**
 * Copyright (c) 2018,2345
 * 摘    要：首页程序
 * 作    者：林幸亿
 * 修改日期：2018.04.20
 */
/*
  require_once 'MysqlDbClass.php';
  require_once 'RedisDbClass.php';
  require_once 'TemplateClass.php';

  /*
 * Retrieve all the user details from DB and display it all in the table
 */
/*
  $db = Mysqldb::getInstance();
  $sql = 'SELECT id, name, email, mobile FROM users ORDER BY id DESC';

  /*
 * Using template to display the table
 */
/*
  $template = new TemplateView();
  $template->users = $db->finds($sql);
  $template->render('table.phtml');
 */
?>

                <?php
                $executionTime = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
                echo '花费时间 Time Taken: ' . number_format($executionTime, 6) . ' seconds';
                
                ?>
                <br><br>
            </div>
        </div> <!-- /container -->
    </body>
</html>
